<?php
class weeks_model extends MY_Model {
	private $module = 'weeks';
	private $table = 'weeks';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$content= $this->input->post('content');
			$this->db->where("(`name` like '$content')");
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$content= $this->input->post('content');
			$this->db->where("(`name` like '$content')");
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function saveManagement($fileName = ''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}
        
   
    	if($this->input->post('finalAdmincp')=='on'){
    			$final = 1;
   		}else{
    			$final = 0;
   		}
    
    	if($this->input->post('activeAdmincp')=='on'){
    			$active = 1;
    		}else{
    			$active = 0;
    		}

		if($this->input->post('hiddenIdAdmincp')==0){
			$data = array(
				'name'=> $this->input->post('name'),
				'status'=> $status,
                'finish'=> $final,
                'active'=> $active,
				'datestart'=> date('Y-m-d 00:00:00',strtotime($this->input->post('datestart'))),
				'dateend'=> date('Y-m-d 23:59:59',strtotime($this->input->post('dateend'))),
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));


			
			$data = array(
				'name'=> $this->input->post('name'),
				'status'=> $status,
                'finish'=> $final,
                'active'=> $active,
				'datestart'=> date('Y-m-d 00:00:00',strtotime($this->input->post('datestart'))),
				'dateend'=> date('Y-m-d 23:59:59',strtotime($this->input->post('dateend'))),
			);
		
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
    
    function getPrices(){
		$this->db->select('*');
		$this->db->where('status',1);
        $this->db->where('chungcuoc',0);
		$this->db->order_by('id','esc');
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
    function getChungCuoc(){
		$this->db->select('*');
		$this->db->where('status',1);
        $this->db->where('chungcuoc',1);
		$this->db->order_by('id','DESC');
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
}